var Common = {
    /** 获取id的拼接字符串，分隔符为',' */
    "getIds": function (rows, attr) {
        if (rows == undefined || rows == null || rows == "") {
            return "0";
        }
        attr = (attr || "id");
        var len = rows.length;
        var ids = "";
        for (var i = 0; i < len; i++) {
            ids += rows[i][attr] + ",";
        }
        if (ids != "") {
            ids = ids.substr(0, ids.length - 1);
        }
        return ids;
    },
    /** 生成默认的按钮权限,根据btnName来排除已经有的 */
    "genFormButtons": function (formName, tableName, rows) {
        var defaultBts = [
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "query",
                "btnTitle": "查询",
                "btnIcon": "icon-query",
                "showOrder": 1,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            },
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "save",
                "btnTitle": "新增",
                "btnIcon": "icon-add",
                "showOrder": 2,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            },
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "edit",
                "btnTitle": "修改",
                "btnIcon": "icon-edit",
                "showOrder": 3,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            },
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "remove",
                "btnTitle": "删除",
                "btnIcon": "icon-remove",
                "showOrder": 4,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            },
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "refresh",
                "btnTitle": "刷新",
                "btnIcon": "icon-refresh",
                "showOrder": 5,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            },
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "import",
                "btnTitle": "导入",
                "btnIcon": "icon-import",
                "showOrder": 6,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            },
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "export",
                "btnTitle": "导出",
                "btnIcon": "icon-export",
                "showOrder": 7,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            },
            {
                "id": 0,
                "formName": formName,
                "tableName": tableName,
                "btnName": "print",
                "btnTitle": "打印",
                "btnIcon": "icon-print",
                "showOrder": 8,
                "btnType": 0,
                "roles": null,
                "isShow":0,
                "btnAction": "",
                "actionScript": ""
            }
        ];

        if (rows == undefined || rows.length == 0) {
            return defaultBts;
        }
        var buttons = [];
        for (var index = 0; index < defaultBts.length; index++) {
            var isEq = false;
            // 名称相同的就排除
            for (var i = 0; i < rows.length; i++) {
                isEq = false;
                if (rows[i].btnName == defaultBts[index].btnName) {
                    isEq = true;
                    break;
                }
            }

            if (isEq == false) {
                buttons.push(defaultBts[index])
            }
        }

        for (var i = 0; i < rows.length; i++) {
            buttons.push(rows[0]);
        }

        return buttons;
    },
    "iconFormatter": function (value, row, index) {
        return "<span class='" + value + "' style='width: 16px;height: 16px;display:block' />";
    },
    "formSubmit": function () {
        $("#form").ajaxForm({
            "url": "ae",
            "method": "post",
            "dataType": "json",
            // "beforeSubmit": function () {
            //     // 表单有效性验证
            //     return $("#form").form("validate");
            // },
            "success": function (data) {
                if (data) {
                    AlertUtil.info(data.msg)
                    if (data.code == 1) {
                        //关闭当前iframe层
                        //先得到当前iframe层的索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //再执行关闭
                        parent.layer.close(index);
                    }
                } else {
                    AlertUtil.error("处理错误，请稍后再试！")
                }
            }
        }).submit();
    },
    getTreeChecked: function (roots) {
        if (roots == null || roots == "") {
            return "";
        }
        var rLen = roots.length;
        for (var rIndex = 0; rIndex < rLen; rIndex++) {
            var value = roots[rIndex];
            if (value) {

                console.log(value)
                if (value.checkState == "indeterminate" || value.checkState == "checked") {
                    return "{id:" + value.id + ",value:'" + value.menuName + "'}"
                }

                if (value.children != null && value.children.length > 0) {
                    Common.getTreeChecked(value.children);
                }
            }
        }
    },
    "permissons": [
        {"value": "index", "label": "显示"},
        {"value": "add", "label": "新增"},
        {"value": "edit", "label": "修改"},
        {"value": "del", "label": "删除"},
        {"value": "list", "label": "列表"},
        {"value": "page", "label": "分页"}
    ]
}